<?php
/**
 * created by zzy
 * date: 2017/10/20 14:58
 */

namespace App\Eloquent\Ygt;

class UserToken extends DbEloquent{

    public static $orderby_id     = false;
    protected   $table      = 'ygt_user_token';

    protected $primaryKey = 'user_id';
    public    $incrementing = false;

    //入库一条数据 或者更新一条数据
    public static function saveOneData( $data ){

        $where          = [ 'user_id'=>$data['user_id'] ];
        $info           = self::getInfo( $where );
        if( !$info ){
            $result     = self::insertOneData( $data );
        } else {
            $result     = self::updateOneData( $where, $data );
        }
        return $result;
    }
    //  验证token
    // type 为1 时默认值 验证设备号
    // type 为2 时 不验证设备号 等价于 checkTokenOnly
    // 返回值 true false
    public static function checkToken( $userId, $imei, $token, $type=1 ){

        $tokenInfo     = self::getTokenInfo( $userId );
        $result         = false;
        if( $tokenInfo ){
            $userToken     = $tokenInfo->user_token;
            $userImei      = $tokenInfo->imei;
            $userIsLogin  = $tokenInfo->is_login;
            //$userIsLogin 1登录状态
            if( $userIsLogin == 1 ){
                switch( $type ){
                    case 1:
                        if( $userToken && $userToken == $token && $userImei == $imei ){
                            $result     = true;
                        }
                        break;
                    case 2:
                        if( $userToken && $userToken == $token  ){
                            $result     = true;
                        }
                        break;
                    default:
                        $result         = false;
                }
            }
        }
        return $result;
    }
    //只验证 token 不验证设备号
    // 返回值 true false
    public static function checkTokenOnly( $userId, $token ){
        $result             = self::checkToken( $userId, '', $token, 2 );
        return $result;
    }
    //根据user_id取token
    //$limit_time 限制token的过期时间 0 表示永不过期
    //返回值 包含 'user_token' 的一个对象集合
    public static function getTokenInfo( $userId, $limitTime=0 ){

        $where  		= [ 'user_id'=>$userId ];
        $data			= self::getInfo( $where );
        if( $data ){
            $userId		= $data->user_id;
            $imei			= $data->imei;
            $salt			= $data->salt;
            $userToken     = self::tokenEncode( $userId, $imei, $salt );

            if( $limitTime > 0 ){
                $lastTime		= $data->last_time;
                $endTime		= $lastTime + $limitTime;
                $nowTime		= $_SERVER['REQUEST_TIME'];
                if( $endTime < $nowTime ){
                    $userToken     = '';
                }
            }
            $data->user_token   = $userToken;
        }
        return $data;
    }
    //加密对应的字段值生成token
    public static function tokenEncode( $userId, $imei, $salt='' ){

        $str            = $userId.$imei.$salt;
        $result         = md5( $str );
        return $result;
    }
}